<?php

namespace app\agent\controller;

use app\agent\model\Recharge as RechargeModel;
use app\agent\validate\Recharge as RechargeValidate;

class Recharge extends Controller
{
    /**
     * 列表.
     */
    public function list()
    {
        $model = new RechargeModel();
        $data = input('post.');
        $data['agent'] = $this->agent;

        return $model->getList($data);
    }

    /**
     * 获取充值二维码
     */
    public function create()
    {
        $data = input('post.');
        $validate = new RechargeValidate();
        if (!$validate->scene('create')->check($data)) {
            return fail($validate->getError());
        }

        $model = new RechargeModel();
        $data['agent'] = $this->agent;

        return $model->create($data);
    }

    /**
     * 支付宝扫码充值【固定二维码】.
     */
    public function queryOrderByTradeNo()
    {
        $data = input('post.');
        $validate = new RechargeValidate();
        if (!$validate->scene('queryOrderByTradeNo')->check($data)) {
            return fail($validate->getError());
        }

        $model = new RechargeModel();
        $data['agent'] = $this->agent;

        return $model->queryOrderByTradeNo($data);
    }

    /**
     * 支付宝扫码充值【呆萌价收款码】.
     */
    public function queryDmjOrderByTradeNo()
    {
        $data = input('post.');
        $validate = new RechargeValidate();
        if (!$validate->scene('queryDmjOrderByTradeNo')->check($data)) {
            return fail($validate->getError());
        }

        $model = new RechargeModel();
        $data['agent'] = $this->agent;

        return $model->queryDmjOrderByTradeNo($data);
    }

    /**
     * 检查是否已经支付.
     */
    public function checkPaid()
    {
        $outTradeNo = input('post.out_trade_no', '');
        $type = input('post.pay_type', 'alipay');
        if (empty($outTradeNo)) {
            return success('还未支付', [
                'is_paid' => 0,
                'trade_no' => '',
            ]);
        }
        if ($type == 'alipay') {
            $model = new \app\common\model\Alipay();
        } elseif ($type == 'wxpay') {
            $model = new \app\common\model\Wxpay();
        } else {
            return success('还未支付', [
                'is_paid' => 0,
                'trade_no' => '',
            ]);
        }
        $res = $model->checkPaid($outTradeNo);
        if (false === $res) {
            return success('还未支付', [
                'is_paid' => 0,
                'trade_no' => '',
            ]);
        }
        $res['is_paid'] = 1;

        return success('支付成功', $res);
    }

    /**
     * 收款码显示哪个
     */
    public function qrcode()
    {
        $qrcode = \think\facade\Db::name('config')
            ->where('field', 'qrcode')
            ->value('value');
        if (empty($qrcode)) {
            $qrcode = 1;
        }
        // 1厦门尖椒 2临下
        return success('操作成功', [
            'qrcode' => $qrcode
        ]);
    }

    /**
     * 退款配置
     */
    public function getConfig()
    {
        $info = \think\facade\Db::name('agent_refund_config')
            ->field('memo')
            ->where('agent_id', 0)
            ->find();
        $info['memo'] = nl2br($info['memo']);
        return success('操作成功', $info);
    }
}
